package top.truehaku.generator.code.infrastructure.persistence.dao.impl;

import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
import top.truehaku.generator.code.infrastructure.persistence.dao.TableDao;
import top.truehaku.generator.code.infrastructure.persistence.entity.TableDO;

import javax.annotation.Resource;

@Repository
public class TableDaoImpl implements TableDao {

    @Resource(name = "template")
    private JdbcTemplate jdbcTemplate;

    @Override
    public TableDO selectByTableName(String tableSchema,String tableName) {
        String sql = "SELECT\n" +
                "\ttbl.table_schema as tableSchema, \n" +
                "\ttbl.table_name as tableName, \n" +
                "\ttbl.table_comment as tableComment, \n" +
                "\ttbl.create_time as createTime\n" +
                "FROM\n" +
                "\tinformation_schema.TABLES tbl \n" +
                "WHERE\n" +
                "\ttbl.table_schema = ? \n" +
                "\tAND tbl.table_name = ? ";
        return jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<>(TableDO.class),tableSchema,tableName);
    }

}
